package net.dgg.rz.production.controller;

import net.dgg.framework.tac.utils.bean.DggRestResponse;
import net.dgg.framework.tac.utils.web.commmon.DggBaseController;
import net.dgg.iboss.base.service.CmsService;
import net.dgg.rz.common.constant.ModuleCodeConstant;
import net.dgg.rz.common.constant.ScProductConstant;
import net.dgg.rz.common.service.CommonMangerExtService;
import net.dgg.rz.common.utils.DataTableResponse;
import net.dgg.rz.common.utils.ExcelTool;
import net.dgg.rz.production.entity.dto.ProcessingOrderDetatilDto;
import net.dgg.rz.production.entity.dto.ProductOrderDetatilDto;
import net.dgg.rz.production.service.ProductDetatilService;
import net.dgg.tmd.foundation.platform.treebook.entity.TreeBook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @className ProductDetatilController
 * @Author Quanlin Wan
 * @Description //TODO 融资生产明细数据Controller
 * @Date 2019/4/28 10:40
 **/
@Controller
@RequestMapping(value = "/rzsc/detatil")
public class ProductDetatilController extends DggBaseController {
    @Autowired
    ProductDetatilService productDetatilService;
    @Autowired
    CommonMangerExtService commonMangerExtService;

    /**
     * @Author Quanlin Wan
     * @Description //TODO 已放款明细页面
     * @Date 14:53 2019/5/6
     * @Param 
     * @return 
     **/
    
    @RequestMapping("/loan_detatil.html")
    public String loanDetatil(Map map) {
        return "rzsc/productOrderDetatil/loan_detailed";
    }

    /**
     * @Author Quanlin Wan
     * @Description //TODO 已放款明细列表数据
     * @Date 14:53 2019/5/6
     * @Param
     * @return
     **/

    @RequestMapping("/loan_detatli_data.do")
    @ResponseBody
    public DggRestResponse loanDetatilData(@RequestParam Map params) {
        List<ProductOrderDetatilDto> productOrderDetatilDto = productDetatilService.loanedScProductOrderWithPage(params);
        return new DataTableResponse().data(params, productOrderDetatilDto);
    }

    /**
     * 导出(已放款明细)
     */
    @RequestMapping(value = "/excel_export_product_detatil.do")
    @ResponseBody
    public void exportDetailExcelAllot(@RequestParam Map params) {
        int[] widths = new int[] { 600*8, 600*8,512*8, 512*8,512*8, 512*8, 512*8, 512*8, 512*8, 512*8, 512*8, 512*8, 512*8, 512*8, 512*8, 512*8, 512*8, 512*8, 512*8, 512*8, 512*8, 512*8, 512*8};
        int[] styleArr = new int[] { -1, -1,0, -1, 0, -1 , -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0};
        List<ProductOrderDetatilDto> productOrderDetatilDtos = productDetatilService.loanedScProductOrderWithPage(params);
        for(ProductOrderDetatilDto productOrderDetatilDto:productOrderDetatilDtos){
            String wayOfLoan = productOrderDetatilDto.getWayOfLoan();
            productOrderDetatilDto.setWayOfLoan(ModuleCodeConstant.paymentMethod.get(wayOfLoan));
        }
        String sheetName  = "已放款明细数据";
        String[] title = new String[]{"接单日期", "放款日期","客户姓名","签单人部门","签单商务","生产所属事业部",
                "流程人员部门","流程人员","生产状态","业务类型","产品名称","商品属性","渠道","利息(%)",
                "还款方式","客户经理","需求金额（元）","放款金额","放款时长","还款时间","到期日","合同编号"};
        String[] keyArray = new String[] {"receiveOrderDate","loanTime","customerName","signUserOrgName","signUserName",
                "productOrgName","flowUserOrgName","flowUserName","status","businessProductName","productName","goodsName",
                "channelName","orderLoansInterest","wayOfLoan","relationshipManager","money","loanAmount","loanTimeDay",
                "dateOfLoan","repaymentDate","contractNo"};
        ExcelTool.execlExport(productOrderDetatilDtos,sheetName,title,keyArray,widths,styleArr);
    }

    /**
     * @Author Quanlin Wan
     * @Description //TODO 跟进中明细页面
     * @Date 14:56 2019/5/6
     * @Param
     * @return
     **/
    @RequestMapping("/processing_detatil.html")
    public String processingDetatil(Map map, Model model) {
        List<TreeBook> treeBooks= commonMangerExtService.queryTreeBookListByCode(ScProductConstant.BUS_YT_DK, 2, 2);
        model.addAttribute("list", treeBooks);
        return "rzsc/productOrderDetatil/processing_order_detatil";
    }

    /**
     * @Author Quanlin Wan
     * @Description //TODO 抵押贷款子页面
     * @Date 15:24 2019/5/6
     * @Param
     * @return
     **/
    @RequestMapping("/loan_on_mortgage.html")
    public String loanOnMortgage(Map map) {
        return "rzsc/productOrderDetatil/loan_on_mortgage";
    }

    /**
     * @Author Quanlin Wan
     * @Description //TODO 信用贷款子页面
     * @Date 11:12 2019/5/7
     * @Param
     * @return
     **/
    @RequestMapping("/.html")
    public String loanOnCredit(Map map) {
        return "rzsc/productOrderDetatil/loan_on_credit";
    }

    /**
     * @Author Quanlin Wan
     * @Description //TODO 资金业务子页面
     * @Date 11:12 2019/5/7
     * @Param
     * @return
     **/
    @RequestMapping("/capital_business.html")
    public String capitalBusiness(Map map) {
        return "rzsc/productOrderDetatil/capital_business";
    }


    /**
     * @Author Quanlin Wan
     * @Description //TODO 跟进中明细列表数据
     * @Date 14:55 2019/5/6
     * @Param 
     * @return 
     **/
    @RequestMapping("/processing_detatil_data.do")
    @ResponseBody
    public DggRestResponse processingDetatilData(@RequestParam Map params) {
        List<ProcessingOrderDetatilDto> processingOrderDetatilDtos = productDetatilService.processingScProductOrderWithPage(params);
        return new DataTableResponse().data(params, processingOrderDetatilDtos);
    }

    /**
     * 导出(跟进中明细)
     */
    @RequestMapping(value = "/excel_export_processing_detatil.do")
    @ResponseBody
    public void excelExportProcessingDetailed(@RequestParam Map params) {
        int[] widths = new int[]{700 * 8, 700 * 8, 700 * 8, 700 * 8, 700 * 8, 700 * 8, 700 * 8, 700 * 8,
                700 * 8, 700 * 8, 700 * 8, 700 * 8, 700 * 8, 700 * 8, 700 * 8, 700 * 8, 700 * 8, 700 * 8,
                700 * 8, 700 * 8, 700 * 8, 700 * 8, 700 * 8, 700 * 8, 700 * 8, 700 * 8, 700 * 8};
        int[] styleArr = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        List<ProcessingOrderDetatilDto> processingOrderDetatilDtos = productDetatilService.processingScProductOrderWithPage(params);
        for(ProcessingOrderDetatilDto processingOrderDetatilDto:processingOrderDetatilDtos){
            String wayOfLoan = processingOrderDetatilDto.getWayOfLoan();
            processingOrderDetatilDto.setWayOfLoan(ModuleCodeConstant.paymentMethod.get(wayOfLoan));
        }
        String target = String.valueOf(params.get("target"));
        String sheetName = new String();
        String[] title = null;
        String[] keyArray = null;
        switch (target){
            case "DYDK":
                sheetName  = "跟进中明细数据（抵押贷款）";
                title = new String[]{"接单日期", "流程人员","流程人员部门","当前完成节点","客户姓名","签单商务",
                        "签单人部门","生产所属事业部","生产单号","业务类型","产品名称","商品属性","渠道","产权性质",
                        "地址","面积","评估公司","评估价格","征信","申请金额","签申请时间","报审时间","通过时间",
                        "通过金额","按揭/全款","取他权时间","办抵押时间"};
                keyArray = new String[] {"receiveOrderDate","flowUserName","flowUserOrgName","latestEndNodeName","customerName",
                        "signUserName","signUserOrgName","productOrgName","scProductOrderNo","businessProductName","productName","goodsName",
                        "channelName","interestTypeName","address","area","escrowCompany","appraisalCost","credit",
                        "appliedAmount","applicationTime","reportTime","approveTime","approveMoney","refundTypeName","takeHisTime","mortgageTime"};
                break;
            case "ZJYW":
                sheetName  = "跟进中明细数据（资金业务）";
                title = new String[]{"接单日期", "流程人员","流程人员部门","当前完成节点","客户姓名","签单商务",
                        "签单人部门","生产所属事业部","生产单号","业务类型","产品名称","商品属性","渠道","需求金额",
                        "合同编号"};
                keyArray = new String[] {"receiveOrderDate","flowUserName","flowUserOrgName","latestEndNodeName","customerName",
                        "signUserName","signUserOrgName","productOrgName","scProductOrderNo","businessProductName","productName","goodsName",
                        "channelName","loanAmount","contractNo"};
                break;
            case "XYDK":
                sheetName  = "跟进中明细数据(信用贷款)";
                title = new String[]{"接单日期", "流程人员","流程人员部门","当前完成节点","客户姓名","签单商务",
                        "签单人部门","生产所属事业部","生产单号","业务类型","产品名称","商品属性","渠道","需求金额",
                        "合同编号","完善资料日期","报审日期","通过时间","通过金额（元）"};
                keyArray = new String[] {"receiveOrderDate","flowUserName","flowUserOrgName","latestEndNodeName","customerName",
                        "signUserName","signUserOrgName","productOrgName","scProductOrderNo","businessProductName","productName","goodsName",
                        "channelName","loanAmount","contractNo","complateDateTime","reportTime","approveTime","approveMoney"};
                break;
            default:
                break;
        }
        for(ProcessingOrderDetatilDto processingOrderDetatilDto:processingOrderDetatilDtos){
            String interestType = String.valueOf(processingOrderDetatilDto.getRefundType());
            processingOrderDetatilDto.setInterestTypeName(ModuleCodeConstant.interestType.get(interestType));
            String refundType = String.valueOf(processingOrderDetatilDto.getRefundType());
            processingOrderDetatilDto.setRefundTypeName(ModuleCodeConstant.refundType.get(refundType));
        }

        ExcelTool.execlExport(processingOrderDetatilDtos,sheetName,title,keyArray,widths,styleArr);
    }
}
